﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.BLL;
using System.Data;
using System.Configuration;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Web;
namespace IHISCETeim.SendSMSSvr
{
    public enum SMSType
    { 
        生日短信,
        机器保养警报,
        通信录代理商短信群发,
        通信录多选批量发送,
        用户来电400,
        程序报警,
        索赔件发货
    }
    public enum SendType
    { 
        单条发送,
        多手机对一内容,
        多手机对多内容
    }

   

    /// <summary>
    /// 处理系统所有短信内容
    /// </summary>
    public class ProccessSysSMS
    {
        private static string _birthdaysmstempletekey = "";
        protected static string BirthdaySMSTempletekey
        {
            get
            {
                try
                {
                    if (string.IsNullOrEmpty(_birthdaysmstempletekey))
                        _birthdaysmstempletekey = ConfigurationManager.AppSettings["birthdaysmstempletekey"];

                    return _birthdaysmstempletekey;
                }
                catch
                {
                    Common.WriteLog("生日短信发送失败[未找到生日短信模板相应配置]");
                    return "";
                }
            }
        }


        public static string StateProccess()
        {
            try
            {
                ProccessBirthday();
                //ProccessMacKeep();
                ProccessSMSProccess();
            }
            catch(Exception exc)
            {
                Common.WriteLog(exc.Message);
                return exc.Message;
            }
            return "";
        }

        /// <summary>
        /// 1.生日短信处理,系统自动判断
        /// 一天只能执行一次
        /// </summary>
        protected static void ProccessBirthday()
        {
            string SMSSouce=DateTime.Now.ToString("yyyy-MM-dd")+"生日短信处理";
            DataTable ssDT=   SMSProccessController.GetBySMSSouce(SMSSouce);
            if (ssDT.Rows.Count > 0)//有记录就不用处理了
                return;
         
            int SSID= SMSProccessController.Add(SMSSouce, "", "");
            int TotalCount = 0;
            string sqlwhere = string.Format("Birthday='{0}'", DateTime.Now.ToString("MM-dd"));
            DataTable dt = UWarrantyCardController.GetPagination(sqlwhere, 1, 999999, " UWCID desc ", ref TotalCount)[0];
            Common.WriteLog("本次需要处理生日短信为" + dt.Rows.Count.ToString());
            foreach (DataRow dr in dt.Rows)
            {
                int UWCID = Convert.ToInt32(dr["UWCID"]);
                string mobile = dr["CustomerMobile"].ToString();
                string smsmessage = USMSTempleteController.GetSMSContent(UWCID, BirthdaySMSTempletekey);

                SMSListInfo obj =new SMSListInfo();
                obj.Mobile=mobile;
                obj.SMSContent=smsmessage;
                obj.SMSType = SMSType.生日短信.ToString();
                obj.SendType = SendType.单条发送.ToString();
                obj.NeedSendDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 9:30:00"));
                DAL.SMSList_Add(obj);
              
            }
            SMSProccessController.Update(SSID);

           
        }

        /// <summary>
        /// 2.机器保养警报短信
        /// </summary>
        protected static void ProccessMacKeep()
        {
            DataTable umdt = UMacKeepController.UMacKeepAlarm_GetALL();
            Common.WriteLog("本次需要处理机器保养警报短信为" + umdt.Rows.Count.ToString());
            Common.WriteLog("暂停保养短信发送2011-11-23");
            return;

            foreach (DataRow dr in umdt.Rows)
            {
                int UMKID = Convert.ToInt32(dr["UMKID"].ToString());
                string smsmessage = dr["AlarmSMSContent"].ToString();
                string mobile = dr["CustomerMobile"].ToString();
                DateTime AlarmDate = Convert.ToDateTime(dr["AlarmDate"].ToString());
                SMSListInfo obj = new SMSListInfo();
                obj.Mobile = mobile;
                obj.SMSContent = smsmessage;
                obj.SMSType = SMSType.机器保养警报.ToString();
                obj.SendType = SendType.单条发送.ToString();
                obj.NeedSendDate = Convert.ToDateTime(AlarmDate.ToString("yyyy-MM-dd 9:30:00"));
                DAL.SMSList_Add(obj);
                UMacKeepController.UMacKeepAlarm_UpdateSendSMS(UMKID);
            }
        }
        /// <summary>
        /// 3.通信录短信发送处理
        /// </summary>
        protected static void ProccessSMSProccess()
        {
            DataTable dt=  SMSProccessController.GetNeedPorccessAll();
            Common.WriteLog("本次需要处理通信录短信发送处理短信为" + dt.Rows.Count.ToString());
            foreach (DataRow dr in dt.Rows)
            {
                int ID = Convert.ToInt32(dr["id"].ToString());
                string   SMSSouce = dr["SMSSouce"].ToString();
                string ObjId= dr["ObjId"].ToString();
                string SMSTempleteKeyOrContent = dr["SMSTempleteKeyOrContent"].ToString();

                switch (SMSSouce)
                { 
                    case "通信录代理商短信群发":
                        SendAgentSMSGroup(ObjId, SMSTempleteKeyOrContent);
                        break;
                    case "通信录多选批量发送":
                        SendCustomerSMSGroup(ObjId, SMSTempleteKeyOrContent);
                        break;
                    case "用户来电400":
                        Proccess4008Record(ObjId, SMSTempleteKeyOrContent);
                        break;
                    case "程序报警":
                        程序报警(ObjId, SMSTempleteKeyOrContent);
                        break;
                    case "索赔件发货":
                        索赔件发货(ObjId, SMSTempleteKeyOrContent);
                        break;
                }
                SMSProccessController.Update(ID);
            }
        }
        /// <summary>
        /// 通信录代理商短信群发处理
        /// </summary>
        /// <param name="AgentNames"></param>
        /// <param name="smskey"></param>
        private static void SendAgentSMSGroup(string AgentNames, string smskey)
        {  
            List<UWarrantyCardExInfo> listUWCE = UWarrantyCardExController.GetByAgentName(AgentNames);
            List<string> mobiles =new List<string>();
            List<string> smscontents = new List<string>();
         
            foreach (UWarrantyCardExInfo item in listUWCE)
            {
                //第一联系人
                if (!item.CustomerName.IsEmptyOrNull() && !item.CustomerMobile.IsEmptyOrNull())
                {                  
                    if (!mobiles.Contains(item.CustomerMobile))
                    {
                        mobiles.Add(item.CustomerMobile);
                        string smsmessage = USMSTempleteController.UWarrantyCardReplaceTagSys(item, smskey);
                        smscontents.Add(smsmessage);
                    }                  
                }
                //第二联系人
                if (!item.CustomerName2.IsEmptyOrNull() && !item.CustomerMobile2.IsEmptyOrNull())
                {
                    if (!mobiles.Contains(item.CustomerMobile2))
                    {
                        mobiles.Add(item.CustomerMobile2);
                        string smsmessage = USMSTempleteController.UWarrantyCardReplaceTagSys(item, smskey, 2);
                        smscontents.Add(smsmessage);
                    }    
                }
                //第三联系人
                if (!item.CustomerName3.IsEmptyOrNull() && !item.CustomerMobile3.IsEmptyOrNull())
                {
                    if (!mobiles.Contains(item.CustomerMobile3))
                    {
                        mobiles.Add(item.CustomerMobile3);
                        string smsmessage = USMSTempleteController.UWarrantyCardReplaceTagSys(item, smskey, 3);
                        smscontents.Add(smsmessage);
                    }  
                }
                
            }

            SMSListInfo obj = new SMSListInfo();
            obj.Mobile = string.Join<string>(",", mobiles);
            obj.SMSContent = string.Join<string>(",", smscontents); ;
            obj.SMSType = SMSType.通信录代理商短信群发.ToString();
            obj.SendType = SendType.多手机对多内容.ToString();
            //obj.NeedSendDate = Convert.ToDateTime(AlarmDate.ToString("yyyy-MM-dd 9:30"));
            DAL.SMSList_Add(obj);

        }

        /// <summary>
        /// 通信录多选批量发送
        /// </summary>
        /// <returns></returns>
        private static void SendCustomerSMSGroup(string uwcids, string smskey)
        {
            string[] UWCIDS = uwcids.Split(new char[] { ',' });
            List<string> mobiles = new List<string>();
            List<string> smscontents = new List<string>();

            foreach (string uwcid in UWCIDS)
            {
                if (uwcid.IsEmptyOrNull())
                    continue;
                UWarrantyCardExInfo item = UWarrantyCardExController.GetByUWCID(Convert.ToInt32(uwcid));
                if (item == null)
                    continue;

                //第一联系人
                if (!item.CustomerName.IsEmptyOrNull() && !item.CustomerMobile.IsEmptyOrNull())
                {
                    if (!mobiles.Contains(item.CustomerMobile))
                    {
                        mobiles.Add(item.CustomerMobile);
                        string smsmessage = USMSTempleteController.UWarrantyCardReplaceTagSys(item, smskey);
                        smscontents.Add(smsmessage);
                    }
                }
                //第二联系人
                if (!item.CustomerName2.IsEmptyOrNull() && !item.CustomerMobile2.IsEmptyOrNull())
                {
                    if (!mobiles.Contains(item.CustomerMobile2))
                    {
                        mobiles.Add(item.CustomerMobile2);
                        string smsmessage = USMSTempleteController.UWarrantyCardReplaceTagSys(item, smskey, 2);
                        smscontents.Add(smsmessage);
                    }
                }
                //第三联系人
                if (!item.CustomerName3.IsEmptyOrNull() && !item.CustomerMobile3.IsEmptyOrNull())
                {
                    if (!mobiles.Contains(item.CustomerMobile3))
                    {
                        mobiles.Add(item.CustomerMobile3);
                        string smsmessage = USMSTempleteController.UWarrantyCardReplaceTagSys(item, smskey, 3);
                        smscontents.Add(smsmessage);
                    }
                }               
            }
            SMSListInfo obj = new SMSListInfo();
            obj.Mobile = string.Join<string>(",", mobiles);
            obj.SMSContent = string.Join<string>(",", smscontents); ;
            obj.SMSType = SMSType.通信录多选批量发送.ToString();
            obj.SendType = SendType.多手机对多内容.ToString();
            //obj.NeedSendDate = Convert.ToDateTime(AlarmDate.ToString("yyyy-MM-dd 9:30"));
            DAL.SMSList_Add(obj);           
        }

        protected static void Proccess4008Record(string ObjId, string SMSTempleteKeyOrContent)
        {
            OUserInfo obju = OUserController.GetByUserName(ObjId);
            SMSListInfo obj = new SMSListInfo();
            obj.Mobile = obju.Mobile;
            obj.SMSContent = SMSTempleteKeyOrContent;
            obj.SMSType = SMSType.用户来电400.ToString();
            obj.SendType = SendType.单条发送.ToString();
            //obj.NeedSendDate = Convert.ToDateTime(AlarmDate.ToString("yyyy-MM-dd 9:30"));
            DAL.SMSList_Add(obj);
        }


        protected static void 程序报警(string mobile,string smscontent)
        {
            SMSListInfo obj = new SMSListInfo();
            obj.Mobile = mobile;
            obj.SMSContent = smscontent;
            obj.SMSType = SMSType.程序报警.ToString();
            obj.SendType = SendType.单条发送.ToString();
            DAL.SMSList_Add(obj);
        }
        protected static void 索赔件发货(string mobile, string smscontent)
        {
            SMSListInfo obj = new SMSListInfo();
            obj.Mobile = mobile;
            obj.SMSContent = smscontent;
            obj.SMSType = SMSType.索赔件发货.ToString();
            obj.SendType = SendType.单条发送.ToString();
            DAL.SMSList_Add(obj);
        }
    }
}
